<?php
  session_start();
/*
  This page is responsible for presenting a printable summary of parcel details.
  The level of detail depends on whether or not isRegistered = 'true'.
  All of the parcel info is passed in the URL.
*/
?>

<?php
  $left_col = 50;
  if ($_REQUEST['file'] == 'trading') {
    $left_col = 30;
  }
?>
<html>
  <head>
    <link rel="stylesheet" href="css/base-style.css">
    <link rel="stylesheet" href="css/site.css">
    <style>
      body {
        background-color : white;
        padding : 15;
      }
    </style>
  </head>
  <body>
<?php
  $html = array();
  $txt  = array();
  array_push($html
    ,'<table style="width:450px">'
      ,'<tr><table style="width:450px"><tr><td style="padding-bottom:10px"><h2>'.$_REQUEST['title'].'</h2></td><td style="text-align : right"><a href="javascript:window.print()">Print</a> | <a href="javascript:window.location=\'/tmp/'.session_id().'.php\'">Save</a></td></tr></table></td></tr>'
      ,'<tr><td><table id="summary">'
  );
  array_push($txt,$_REQUEST['title']);
  $paramMapping = array(
     'parcelNum'                           => 'Parcel #'
    ,'ownerName'                           => 'Owner Name'
  );
  if ($_REQUEST['isRegistered'] != 'true') {
    $paramMapping = array(
       'ownerAddress'                        => 'Property Address'
    );
  }
  $paramMapping = array_merge($paramMapping,array(
     'depthToHWTIn'                        => 'Depth to High Water Table (in)'
    ,'depthToResLyrIn'                     => 'Depth to Restrictive Layer (in)'
    ,'hydSoilGroup'                        => 'Hydrologic Soil Group'
    ,'kSatInHr'                            => 'Saturated Hydraulic Conductivity (in/hr)'
    ,'impervAreaAc'                        => 'Impervious Area (ac)'
    ,'uplandAreaAc'                        => 'Upland Area (ac)'
    ,'wetlandAreaAc'                       => 'Wetland Area (ac)'
    ,'totalAreaAc'                         => 'Total Area (ac)'
    ,'pervLoadLbYr'                        => 'Pervious Load (lb/yr)'
    ,'impervLoadLbYr'                      => 'Impervious Load (lb/yr)'
    ,'totalLoadLbYr'                       => 'Total Load (lb/yr)'
    ,'loadRemovedLbYr'                     => 'Load Removed (lb/yr)'
    ,'stormwaterControl'                   => 'Stormwater Control'
    ,'loadReductionPct'                    => 'Load Reduction (%)'
    ,'totalImperviousAreaAc'               => 'Total Impervious Area (ac)'
    ,'waterQualirtyDepthIn'                => 'Water Quality Depth (in)'
    ,'stormwaterControlTotalCostDollar'    => 'Stormwater Control Total Cost ($)'
    ,'stormwaterControlUnitCostDollarLbYr' => 'Stormwater Control Unit Cost ($/lb/yr)'
    ,'town'                                => 'Town'
    ,'creditDeficit'                       => 'Credit/Deficit'
    ,'creditRemovealLbYr'                  => 'Credit Removal (lb/yr)'
    ,'priceDollar'                         => 'Price ($)'
    ,'priceDollarLbYr'                     => 'Price ($/lb/yr)'
    ,'loadCreditDeficitLbYr'               => 'Load Credit/Deficit (lb/yr)'
    ,'creditUnitValueDollarLbYr'           => 'Credit Unit Value ($/lb/yr)'
    ,'siteConstraint'                      => 'Site Constraint'
    ,'mailAddr'                            => 'Mailing Address'
  ));

  foreach ($_REQUEST as $k => $v) {
    if (array_key_exists($k,$paramMapping)) {
      array_push($html,"<tr><td>$paramMapping[$k]</td><td>$v</td></tr>");
      array_push($txt,sprintf("%-".$left_col."s %s",$paramMapping[$k],strip_tags($v)));
    }
    else if (preg_match('/subtitle/',$k)) {
      array_push($html,"<tr><td colspan=2 style=\"padding-bottom:10px;padding-top:10px\"><h3>$v</h3></td></tr>");
      array_push($txt,"\n\n$v\n\n");
    }
    else if (preg_match('/blank/',$k)) {
      array_push($txt,"\n");
    }
  }
  array_push($html
      ,'</table></td></tr>'
    ,'</table>'
  );

  print implode($html,"\n");
?>
  </body>
</html>

<?php
  file_put_contents('/var/www/html/tmp/'.session_id().'.php'
    ,'<?php header("Content-Disposition: attachment; filename=\"'.$_REQUEST['file'].'.'.$_REQUEST['parcelNum'].'.txt\"")?>'
    .implode($txt,"\n")
  );
?>
